package com.example.mark.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.mark.entity.User;
import com.example.mark.mapper.UserMapper;
import com.example.mark.service.UserService;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

@Service // 用于服务层 注册到spring中的bean中 这样AutoWired 才可以找得到
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {
    @Resource //注入依赖 类似AutoWired
    private UserMapper userMapper;


    /**
     * 校验密码强度
     * 需要密码长度至少为 8 位，包含数字、字母和特殊字符
     * */
    public boolean isPasswordStrong(String password) {
        // 密码长度至少8位
        if (password.length() < 8) {
            return false;
        }
        // 包含数字
        boolean hasDigit = false;
        // 包含字母
        boolean hasLetter = false;
        // 包含特殊字符
        boolean hasSpecialChar = false;
        for (char c : password.toCharArray()) {
            if (Character.isDigit(c)) {
                hasDigit = true;
            } else if (Character.isLetter(c)) {
                hasLetter = true;
            } else {
                hasSpecialChar = true;
            }
        }
        return hasDigit && hasLetter && hasSpecialChar;
    }

    /**
     * 根据用户名 判断用户是否存在
     * @param  user
     * @retuen User
    * */
    public User isUserExists(User user){
        LambdaQueryWrapper<User> lqw = new LambdaQueryWrapper<>();
        lqw.eq(User::getUsername,user.getUsername());
        User one = this.getOne(lqw);
        return one;
    }

}
